package com.example.teaching_server.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.PositiveOrZero;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

/**
 * 文件上传响应DTO
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
@Schema(description = "文件上传响应信息")
public class FileUploadResponse {

    @NotNull(message = "上传状态不能为空")
    @Schema(description = "上传是否成功", example = "true", requiredMode = Schema.RequiredMode.REQUIRED)
    private Boolean success;  // 保持Boolean类型，但添加isSuccess()方法

    @NotBlank(message = "响应消息不能为空")
    @Schema(description = "响应消息", example = "文件上传成功", requiredMode = Schema.RequiredMode.REQUIRED)
    private String message;

    @Schema(description = "服务器存储的文件名", example = "uuid-filename.jpg")
    private String fileName;

    @Schema(description = "原始文件名", example = "我的图片.jpg")
    private String originalFileName;

    @Schema(description = "服务器文件路径", example = "/uploads/2024/03/filename.jpg")
    private String filePath;

    @Schema(description = "文件访问URL", example = "https://example.com/uploads/2024/03/filename.jpg")
    private String fileUrl;

    @PositiveOrZero(message = "文件大小不能为负数")
    @Schema(description = "文件大小（字节）", example = "102400")
    private Long fileSize;

    @Schema(description = "文件MIME类型", example = "image/jpeg")
    private String fileType;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Schema(description = "上传时间", example = "2024-03-01 10:30:00")
    private LocalDateTime uploadTime;

    @Schema(description = "错误代码（上传失败时返回）", example = "FILE_TOO_LARGE")
    private String errorCode;

    /**
     * 为了兼容旧的isSuccess()调用，添加此方法
     */
    public boolean isSuccess() {
        return Boolean.TRUE.equals(success);
    }

    /**
     * 设置success字段的便捷方法
     */
    public void setSuccess(boolean success) {
        this.success = success;
    }

    /**
     * 成功响应（基础版本）
     */
    public static FileUploadResponse success(String fileName, String filePath,
                                             String originalFileName, Long fileSize, String fileType) {
        return FileUploadResponse.builder()
                .success(true)
                .message("文件上传成功")
                .fileName(fileName)
                .filePath(filePath)
                .originalFileName(originalFileName)
                .fileSize(fileSize)
                .fileType(fileType)
                .uploadTime(LocalDateTime.now())
                .build();
    }

    /**
     * 成功响应（包含URL版本）
     */
    public static FileUploadResponse success(String fileName, String filePath, String fileUrl,
                                             String originalFileName, Long fileSize, String fileType) {
        return FileUploadResponse.builder()
                .success(true)
                .message("文件上传成功")
                .fileName(fileName)
                .filePath(filePath)
                .fileUrl(fileUrl)
                .originalFileName(originalFileName)
                .fileSize(fileSize)
                .fileType(fileType)
                .uploadTime(LocalDateTime.now())
                .build();
    }

    /**
     * 失败响应
     */
    public static FileUploadResponse error(String message) {
        return FileUploadResponse.builder()
                .success(false)
                .message(message)
                .uploadTime(LocalDateTime.now())
                .build();
    }

    /**
     * 失败响应（包含错误代码）
     */
    public static FileUploadResponse error(String message, String errorCode) {
        return FileUploadResponse.builder()
                .success(false)
                .message(message)
                .errorCode(errorCode)
                .uploadTime(LocalDateTime.now())
                .build();
    }

    /**
     * 获取文件大小的人类可读格式
     */
    @Schema(description = "文件大小（人类可读）", accessMode = Schema.AccessMode.READ_ONLY)
    public String getHumanReadableSize() {
        if (fileSize == null) return "未知";

        if (fileSize < 1024) {
            return fileSize + " B";
        } else if (fileSize < 1024 * 1024) {
            return String.format("%.1f KB", fileSize / 1024.0);
        } else if (fileSize < 1024 * 1024 * 1024) {
            return String.format("%.1f MB", fileSize / (1024.0 * 1024));
        } else {
            return String.format("%.1f GB", fileSize / (1024.0 * 1024 * 1024));
        }
    }
}
